package com.cg.cgms.controller;

import com.cg.cgms.pojo.Food;
import com.cg.cgms.pojo.Result;
import com.cg.cgms.pojo.Shop;
import com.cg.cgms.pojo.UserLove;


import com.cg.cgms.service.FoodService;
import com.cg.cgms.service.ShopService;
import com.cg.cgms.service.UserService;
import com.cg.cgms.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;


/**
 * @author ASUS
 */
@RestController
@RequestMapping("/Shop")
public class ShopController {


    @Autowired
    private ShopService shopService;
    @Autowired
    private FoodService foodService;
    @Autowired
    UserService userService;
    //查询店铺信息
    @GetMapping("/introduce")
    public Result introduce(Integer ssid){
        Shop shop=shopService.findBySsid(ssid);
        return Result.succsee(shop);
    }
    //根据店铺id查询菜品
    @GetMapping("/foodBySsid")
    public Result foodBySsid(Integer ssid){
        ArrayList<Food> foods=foodService.foodBySsid(ssid);
        return Result.succsee(foods);
    }
    //搜索功能，模糊查询店铺名字
    @GetMapping("/findByName")
    public Result findByNane(String name){
        ArrayList<Shop> shops=shopService.findByName(name);
        return Result.succsee(shops);
    }
    //搜索功能，根据店铺种类查询店铺
    @GetMapping("/findByType")
    public Result findByType(String type){
        ArrayList<Shop> shops=shopService.findByType(type);
        return Result.succsee(shops);
    }
    @GetMapping("/code")
    public Result findcode(String code){
        Object openid=shopService.findcode(code);
        return Result.succsee(openid);
    }
//    @GetMapping("/api/getOpenId")
//    public String getOpenId(@RequestParam("code") String code) {//@RequestParam("code") String code
//        RestTemplate restTemplate = new RestTemplate();
//        String urlTemplate = WECHAT_API_URL +
//                "?appid={appid}&secret={secret}&js_code={code}&grant_type=authorization_code";
//       // String code="0b1zLHFa12ge1H0rMTHa1x99bP0zLHFE";
//        String response = restTemplate.getForObject(urlTemplate, String.class, APPID, SECRET, code);
//        // 响应示例：{"session_key":"xxx","openid":"oWgGz4xxxxxx"}
//        System.out.println(response);
//        Gson gson = new Gson();
//        OpenId person = gson.fromJson(response, OpenId.class);
//        System.out.println(person.getOpenid());
//        return response; // 实际开发中，你可能需要解析这个JSON字符串并处理错误情况
//    }


    @GetMapping("/userlova")
    public Result userLvoe(){
        Map<String, Object> map = ThreadLocalUtil.get();
        String openid=(String) map.get("openid");
        ArrayList<UserLove> userLoves=shopService.userLove(openid);
        ArrayList<Shop> shops=new ArrayList<>();
        for (UserLove list : userLoves) {
            shops.add(shopService.findBySsid(list.getSsid()));
        }

        return Result.succsee(shops);
    }
    @GetMapping("/shopbyTA")
    public Result shopbyTA(String address,String type){

        ArrayList<Shop> shops=shopService.findByTA(address,type);


        return Result.succsee(shops);

    }
    @GetMapping("/search")
    public Result search(String name){

        ArrayList<Food> foods=foodService.foodByName(name);
        HashSet<Shop> shops=new HashSet<>();
        for(Food list:foods){
            shops.add(shopService.findBySsid(list.getSsid()));
        }

        return Result.succsee(shops);
    }
}
